
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;


//玩家等待方块执行，根据方块的类型，指定不同的行为模式
public class PlayerWaitBox:MonoBehaviour
{

    public enum BoxpositionType{inner,outside};
    
    public BoxpositionType boxpositionType;


 
    public Sprite[] sprites;





    //方格的类型： 移动，资源，敌人
    public enum BoxType{move,resours,emeny,ban,normal}

    public BoxType boxType;
    public Dictionary<BoxType,Sprite> boxTypeSprite;
    public Dictionary<BoxType, Color32> boxTypeColor;



    //当前碰到的物体-具体的物体
    public GameObject gm;


    public Dictionary<BoxType,string> typedic;


    //在Awake的方法中，取初始化字典的数据
    //在awake中，初始化字典的各项数据
    //转变放个的类型
    Box boxImpl = null;

    private void Awake()
    {
        boxTypeColor = new Dictionary<BoxType, Color32>();
        boxTypeSprite = new Dictionary<BoxType, Sprite>();
        // 禁止移动
        boxTypeColor.Add(BoxType.ban, new Color32(255, 255, 255, 30));
        boxTypeSprite.Add(BoxType.ban,sprites[1]);
        // 移动
        boxTypeColor.Add(BoxType.move, new Color32(255, 255, 255, 103));
        boxTypeSprite.Add(BoxType.move,sprites[0]);
        // 敌人
        boxTypeSprite.Add(BoxType.emeny,sprites[0]);
        boxTypeColor.Add(BoxType.emeny, new Color32(255, 0, 0, 103));
        // 资源
        boxTypeSprite.Add(BoxType.resours,sprites[0]);
        boxTypeColor.Add(BoxType.resours, new Color32(229, 255, 0, 141));


    }


    private void Update()
    {

        transform.rotation = Camera.main.transform.rotation;

    }

    public void clearState()
    {

        boxType = BoxType.move;
        gameObject.GetComponent<SpriteRenderer>().sprite = boxTypeSprite[boxType];
        gameObject.GetComponent<SpriteRenderer>().DOColor(boxTypeColor[boxType], 0.1f);

    }

    //执行
    public void ExecuteCommand(){
        //获取字典中对应的颜色
         //  typedic.Values(boxType);
        //将方格转换为颜色
        switch(boxType){
            case BoxType.move: 
                boxImpl = new PlayerMove(this.gameObject);
                break;
            case BoxType.emeny:
                boxImpl = new PlayerAttack(this.gameObject);
                boxImpl.setGm(gm);
                break;
            case BoxType.resours:
                boxImpl = new MaterialBox(this.gameObject);
                boxImpl.setGm(gm);
                break;
            default:
                boxType = BoxType.move;
                boxImpl = new PlayerMove(this.gameObject);
                break;
        }
        boxImpl.action();
    }




    // TODO： 触发器
    //根据tag



    private void OnTriggerEnter(Collider other)
    {



        Debug.Log("触发器！");
        //根据触发器改变其颜色和该方块的状态
        if (other.gameObject.tag.Equals("resours"))
        {
            gm = other.gameObject;
            //转变颜色
            boxType = BoxType.resours;
        }
        else if (other.tag.Equals("emeny"))
        {
            gm = other.gameObject;
            float playerEmenyDis = Vector3.Distance(Player.Instance.transform.position, gm.transform.position);
            Debug.Log("触发器！22222" + playerEmenyDis);
            if (playerEmenyDis <= 2f)
            {
                //改变为红色 而且玩家距离敌人的距离还得是小于1
                boxType = BoxType.emeny;
            }
            //调用
            //取颜色字典，改变方块的颜色
        }
        else if (other.tag.Equals("obstacle"))
        {
            //障碍物
            boxType = BoxType.ban;
        }
        else
        {
            boxType = BoxType.move;

        }


        if (boxpositionType.Equals(BoxpositionType.inner) && boxType != BoxType.move)
        {

            string name = gameObject.name;
            //改名 -1 -2 -3
            string beforeName = name.Split("-")[0];
            string outSideName = beforeName + "-" + "2";
            print("名称：" + outSideName);

            GameObject outSideGm = GameObject.Find("PreBoxManger/PreBox/" + outSideName).gameObject;
            if (outSideGm.GetComponent<PlayerWaitBox>().boxType.Equals(BoxType.move))
            {
                //改变为不可移动状态，写乱了
                outSideGm.GetComponent<PlayerWaitBox>().boxType = BoxType.ban;
                outSideGm.GetComponent<SpriteRenderer>().sprite = boxTypeSprite[BoxType.ban];
                outSideGm.GetComponent<SpriteRenderer>().DOColor(boxTypeColor[BoxType.ban], 0.1f);
            }
            //就要影响外部的效果，ban？

        }
        gameObject.GetComponent<SpriteRenderer>().sprite = boxTypeSprite[boxType];
        gameObject.GetComponent<SpriteRenderer>().DOColor(boxTypeColor[boxType], 0.1f);





    }


    private void OnTriggerEnter2D(Collider2D collision){

        Debug.Log("触发器！");
        //根据触发器改变其颜色和该方块的状态
        if (collision.gameObject.tag.Equals("resours"))
        {
            gm = collision.gameObject;
            //转变颜色
            boxType = BoxType.resours;
        } else if (collision.tag.Equals("emeny") ) {
             gm = collision.gameObject;
            float playerEmenyDis= Vector3.Distance(Player.Instance.transform.position,gm.transform.position);
            Debug.Log("触发器！22222"+ playerEmenyDis);
            if (playerEmenyDis<=2f){
                //改变为红色 而且玩家距离敌人的距离还得是小于1
                boxType = BoxType.emeny;
            }
        //调用
        //取颜色字典，改变方块的颜色
        } else if (collision.tag.Equals("obstacle")) {
        //障碍物
        boxType = BoxType.ban;
        }else{
            boxType = BoxType.move;
           
        }


        if(boxpositionType.Equals(BoxpositionType.inner)&& boxType != BoxType.move)
        {

            string name= gameObject.name;
            //改名 -1 -2 -3
            string beforeName= name.Split("-")[0];
            string outSideName=beforeName+"-"+"2";
            print("名称：" + outSideName);
       
            GameObject outSideGm = GameObject.Find("PreBoxManger/PreBox/" + outSideName).gameObject;
            if (outSideGm.GetComponent<PlayerWaitBox>().boxType.Equals(BoxType.move)){
                //改变为不可移动状态，写乱了
                outSideGm.GetComponent<PlayerWaitBox>().boxType = BoxType.ban;
                outSideGm.GetComponent<SpriteRenderer>().sprite = boxTypeSprite[BoxType.ban];
                outSideGm.GetComponent<SpriteRenderer>().DOColor(boxTypeColor[BoxType.ban], 0.1f);
            }
            //就要影响外部的效果，ban？

        }
        gameObject.GetComponent<SpriteRenderer>().sprite = boxTypeSprite[boxType];
        gameObject.GetComponent<SpriteRenderer>().DOColor(boxTypeColor[boxType], 0.1f);

    }



    private void OnTriggerExit2D(Collider2D collision)
    {


        if (boxpositionType.Equals(BoxpositionType.inner) && boxType == BoxType.move)
        {

            string name = gameObject.name;
            //改名 -1 -2 -3
            string beforeName = name.Split("-")[0];
            string outSideName = beforeName + "-" + "2";
            print("名称：" + outSideName);

            GameObject outSideGm = GameObject.Find("Player/PreBox/" + outSideName).gameObject;
            if (outSideGm.GetComponent<PlayerWaitBox>().boxType.Equals(BoxType.ban))
            {
                //改变为不可移动状态，写乱了
                outSideGm.GetComponent<PlayerWaitBox>().boxType = BoxType.move;
                outSideGm.GetComponent<SpriteRenderer>().sprite = boxTypeSprite[BoxType.move];
            }
        }
       
    }



}





//抽象类  如果想要增加一种行为，直接加一个类就行了(才怪呢)
public interface Box{
    public void action(){}
    public void setGm(GameObject gm);

}


//简单工厂模式

//具体的类
public class PlayerMove : Box
{
    GameObject gm;


    public void setGm(GameObject gm) {
        this.gm = gm;
    }

    public PlayerMove(GameObject gm){
        this.gm = gm;
    }


    public void action(){

     Player.Instance.Move(gm.transform.localPosition+Player.Instance.transform.position);
        
    }

}





//具体的类
public class PlayerAttack: Box
{
    GameObject gm;



    public void setGm(GameObject gm)
    {
        this.gm = gm;
    }
    public PlayerAttack(GameObject gm)
    {
        this.gm = gm;
    }

    public void action(){
        //需要攻击的人的位置
        Player.Instance.Attack(gm);
        
    }
    

}



//宝箱资源
public class MaterialBox: Box
{
    GameObject gm;



    public void setGm(GameObject gm)
    {
        this.gm = gm;
    }
    public MaterialBox(GameObject gm)
    {
        this.gm = gm;
    }

    //行为模式
    public void action(){
        //需要攻击的人的位置
        //一个开宝箱的动画+奖励点物资
        //你点击的这个宝箱物体的特殊方法
        gm.GetComponent<SpecialItem>().Excute();
        //销毁物资本身
     //   gm.SetActive(false);

    }
    

}


